package myJs.api

import myJs.companyMember.sendSampleManage.SendSampleManage
import myJs.myPkg.{MyFetch, SwalOptions, layer, swal}
import myJs.tool.Tool
import org.scalajs.dom.{File, FormData, XMLHttpRequest}
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.ValidData
import shared.pojo.Sample._
import slinky.web.html.s

/**
 * Created by yz on 19/5/2022
 */
object SampleApi {

  val baseUrl = s"${Api.apiUrl}/panda/sample"

  def all = {
    val url = s"${Api.apiUrl}/panda/sample/all"
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def lock(sampleId: String) = {
    val url = s"${baseUrl}/lock"
    MyFetch.getRnStr(url = s"${url}?id=${sampleId}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def unlock(sampleId: String) = {
    val url = s"${baseUrl}/unlock"
    MyFetch.getRnStr(url = s"${url}?id=${sampleId}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def getById(idV: String) = {
    val url = s"${Api.apiUrl}/panda/sample/getById"
    MyFetch.getRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json")).map { str =>
      read[SampleInfo](str)
    }
  }

  def getOpById(idV: String) = {
    val url = s"${Api.apiUrl}/panda/sample/getOpById"
    MyFetch.getRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json")).map { str =>
      read[Option[SampleInfo]](str)
    }
  }

  def getAllNoUsedId(request: GetAllNoUsedId.Request) = {
    val url = s"${baseUrl}/getAllNoUsedId"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json")).map { str =>
      read[GetAllNoUsedId.Response](str)
    }
  }

  def getByIdNoImage(idV: String) = {
    val url = s"${Api.apiUrl}/panda/sample/getByIdNoImage"
    MyFetch.getRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json")).map { str =>
      read[SampleInfo](str)
    }
  }

  def delete(idV: String) = {
    val url = s"${Api.apiUrl}/panda/sample/delete"
    MyFetch.deleteRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def update(formData: FormData)(f: String => Unit) = {
    val url = s"${baseUrl}/update"
    val xhr = new XMLHttpRequest
    xhr.open("post", url)
    xhr.onreadystatechange = (e) => {
      if (xhr.readyState == XMLHttpRequest.DONE) {
        val response = xhr.responseText
        f(response)
      }
    }
    xhr.send(formData)
  }

  def add(formData: FormData)(f: String => Unit) = {
    val url = s"${baseUrl}/add"
    val xhr = new XMLHttpRequest
    xhr.open("post", url)
    xhr.onreadystatechange = (e) => {
      if (xhr.readyState == XMLHttpRequest.DONE) {
        val response = xhr.responseText
        f(response)
      }
    }
    xhr.send(formData)
  }

  def freezerInfo(request: FreezerInfo) = {
    val url = s"${baseUrl}/freezerInfo"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json")).map { str =>
      read[FreezerInfo.Response](str)
    }
  }

}
